/**
 * Carnegie Mellon University
 * School of Computer Science
 * MSIT eBusiness Technology
 * 
 * Team 8 - Infinity (2011 Spring)
 * infinity-team8@googlegroups.com
 * 
 * Professor: Jeffrey L. Eppinger
 * Program Faculty: Sujata Telang
 */

package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.EmployeeDAO;
import model.Model;
import model.CustomerDAO;
import model.PositionDAO;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import databean.CustomerBean;
import databean.EmployeeBean;
import databean.PositionBean;
import formbean.EmployeeViewCustomerAccountForm;


public class EmployeeViewCustomerAccountAction extends Action {
	private FormBeanFactory<EmployeeViewCustomerAccountForm> formBeanFactory = FormBeanFactory.getInstance(EmployeeViewCustomerAccountForm.class);

	private CustomerDAO		customerDAO;
	private EmployeeDAO		employeeDAO;
	private PositionDAO		positionDAO;
	
	public EmployeeViewCustomerAccountAction(Model model) {
		customerDAO		= model.getCustomerDAO();
		employeeDAO		= model.getEmployeeDAO();
		positionDAO		= model.getPositionDAO();
	}

	public String getName() { return "employee-view-customer-account.do"; }

    public String perform(HttpServletRequest request,HttpServletResponse response) {
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
        //returns the existing session or null, if the session does not exist.
        HttpSession session = request.getSession(false);

        try {
        	
        	request.setAttribute("employee",(EmployeeBean) request.getSession(false).getAttribute("employee"));
			request.getSession().setAttribute("employee",(EmployeeBean) request.getSession(false).getAttribute("employee"));
        	
	        EmployeeViewCustomerAccountForm form = formBeanFactory.create(request);
	        request.setAttribute("form",form);
	        
	        
	        request.setAttribute("customerList",customerDAO.getCustomers());
	        request.setAttribute("employeeList",employeeDAO.getEmployees());
	        
	        
	        
	        // If no params were passed, return with no errors so that the form will be
	        // presented (we assume for the first time).
	        if (!form.isPresent()) {
	            return "employee-view-customer-account.jsp";
	        }
	
	        // Any validation errors?
	        errors.addAll(form.getValidationErrors());
	        if (errors.size() != 0) {
	            return "employee-view-customer-account.jsp";
	        }
	        
	        CustomerBean customer = customerDAO.lookup(form.getUserName());
	        request.setAttribute("customer",customer);
	        
	        PositionBean[] positions = positionDAO.lookup(customer.getCustomerID());
			request.setAttribute("positions",positions);
	        
	        // After successful registration (and login) send to...
	        String redirectTo = (String) session.getAttribute("redirectTo");
	        if (redirectTo != null) return redirectTo;
	        
	        return "employee-view-customer-account-result.jsp";
        } catch (DAOException e) {
        	errors.add(e.getMessage());
        	return "employee-view-customer-account.jsp";
        } catch (FormBeanException e) {
        	errors.add(e.getMessage());
        	return "employee-view-customer-account.jsp";
        }
    }
}
